/**
 * =================================================================
 * 版权所有 2011-2013 深圳市泰海网络科技服务有限公司，并保留所有权利
 * -----------------------------------------------------------------
 * 这不是一个自由软件！您不能在任何未经允许的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * =================================================================
 */
package com.imoney.payment.trade.handler;

import java.util.List;

import org.springframework.stereotype.Service;

import com.imoney.framework.base.entity.IRequest;
import com.imoney.framework.base.entity.Response;
import com.imoney.framework.base.exception.ServiceException;
import com.imoney.framework.base.utils.StringUtils;
import com.imoney.payment.chain.HandlerChain;
import com.imoney.payment.chain.IHandler;
import com.imoney.payment.trade.common.InfoCode;
import com.imoney.payment.trade.domain.EmailItem;
import com.imoney.payment.trade.domain.TransferRequest;
import com.imoney.payment.trade.enums.PortType;

/**
 * 类说明：检查参数<br>
 * 
 * <p>
 * 详细描述：<br>
 * 
 * </p>
 * 
 * <pre>
 * ——————————————————————————————————————————————————————————————————
 * |		修改人		|		修改时间			|		修改原因
 * ——————————————————————————————————————————————————————————————————
 * |	zengxx 曾宪新	|		2013-6-6		|	
 * ——————————————————————————————————————————————————————————————————
 * </pre>
 * 
 * @author zengxx 曾宪新(Xavier.zeng)
 * 
 *         CreateDate: 2013-6-6
 */
@Service
public class TransferParamHandler implements IHandler<String> {

	@Override
	public void doHandler(HandlerChain<String> chain, IRequest request,
			Response<String> response) throws ServiceException {
		TransferRequest trRequest = (TransferRequest) request;
		response.setCode(InfoCode.PARAM_IS_NULL);
		if (null == trRequest.getTradeType()) {
			response.setMessage("trade type is null!");
			return;
		}
		if (null == trRequest.getBusinessType()) {
			response.setMessage("business type is null!");
			return;
		}
		if (null == trRequest.getProductType()) {
			response.setMessage("product type is null!");
			return;
		}
		if (StringUtils.isEmpty(trRequest.getSubject())) {
			response.setMessage("subject is null!");
			return;
		}
		if (StringUtils.isEmpty(trRequest.getMemo())) {
			response.setMessage("memo is null!");
			return;
		}
		if (StringUtils.isEmpty(trRequest.getFromEmail())) {
			response.setMessage("from email is null!");
			return;
		}
		List<EmailItem> items = trRequest.getEmailItems();
		if (null == items || items.isEmpty()) {
			response.setMessage("to items is null!");
			return;
		}
		for (EmailItem item : items) {
			if (StringUtils.isEmpty(item.getToEmails())) {
				response.setMessage("to email is null!");
				return;
			}
			if (item.getQuantity() <= 0) {
				response.setMessage("quantity must gt 0!");
				return;
			}
			if (item.getAmt() <= 0) {
				response.setMessage("amt must gt 0!");
				return;
			}
			if (null != item.getPortType()
					&& !PortType.NONEED.equals(item.getPortType())) {
				if (item.getAddrId() <= 0) {
					response.setMessage("addrId must gt 0!");
					return;
				}
				if (StringUtils.isEmpty(item.getWhoPay())) {
					response.setMessage("who pay is null!");
					return;
				}
			}
		}
		response.setCode(InfoCode.SUCCEED);
		/** 处理下一个任务 */
		chain.doHandler(chain, request, response);
		response.setMessage(response.getMessage() + " param validate pass!");
	}

}
